import TWEEN from './tween.js';

var ZW_Ctx;
var angle = 0;
var ZW_PicArray = [];
var goodsArray = [];
var ZW_Offset = 30;
var ZW_PointsArray = [];
var ZW_Info;
var ZW_PixelRatio = 2;
var gIsIphone = false;

var ZW_ClickNodeName = '';
var ZW_HoldNodeName = '';       //长按得珠子名称
var ZW_HoldIndex = 0;   //长按得珠子所在的索引值
var ZW_bHold = false;
var ZW_bLoading = false;  //是否正在加载图片

var Scene_OffsetX = 0;
var Scene_OffsetY = 0;

var Scene_ScaleX = 1.0;
var Scene_ScaleY = 1.0;

var ZW_ClickPos;

var ZW_CurrentPos = 0;

var ZW_Close_Url = '../../img/close.png';
var ZW_Scale_Url = '../../img/scale.png';
var ZW_Rot_Url = '../../img/rotation.png';

var ZW_bTipEdit = true;

var ZW_MoveType = '';

var ZW_tipArray = []; //贴纸名称数组，需要保证不重复
var ZW_tipNumArray = [];  //用于贴纸加载时的计数

var that;

function guid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}


function AddChain(tag, url) {
  //AddImage(tag,url,ZW_Info.windowWidth/2,150,200,200,0,1.0);

  AddImage(tag, url, 0, 100, ZW_Info.windowHeight * 0.7882, ZW_Info.windowHeight, 0, 1.0);
}



function AddImage(tag, url, x, y, width, height, rot, scale,visible,order) {
  if(!order) order = 0;
  let contentPicNetPath = url;

  var cImage = { tag: tag, x: x, y: y, width: width, height: height, url: '', rot: rot, scale: scale ,visible:visible,renderOrder:order};
  ZW_PicArray.push(cImage);

  jd.getImageInfo({
    //保存网络图片
    src: contentPicNetPath,
    success:
      function (res) {
        cImage.url = res.path;

        //ZW_PicArray.push({tag:tag,x:x,y:y,width:width,height:height,url:res.path,rot:rot,scale:scale});
        //console.log(ZW_PicArray);
      }
  })
}


function AddExpression(name) {

  for(var i=0; i<5; i++)
    {
      RemoveImage('expression-'+i);
    }

  //   AddImage('expression1', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/expression/ex2/female02-0.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
    //   ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0,true);

  var gifUrlArray = [];
      for(var i=0; i<5; i++)
      {
        gifUrlArray.push(name+ '-'+ i + '.png');

        if(i==0)
        {
          AddGifImage1('expression-'+i, gifUrlArray[i], ZW_Info.windowWidth * 0.526, ZW_Info.windowWidth * 0.462,
          ZW_Info.windowWidth * 0.5333*0.55, ZW_Info.windowWidth * 0.42666*0.55, 0, 1.0,false,4);
        }else
        {
          AddGifImage1('expression-'+i, gifUrlArray[i], ZW_Info.windowWidth * 0.526, ZW_Info.windowWidth * 0.462,
          ZW_Info.windowWidth * 0.5333*0.55, ZW_Info.windowWidth * 0.42666*0.55, 0, 1.0,false,4);
        }
        
      }
}

//判断2d点是否在三角形中。x0, y0，为检测点。剩余三个点，为三角形的三个点。当返回结果为true，则检测点在三角形上。如果为false，则点不在三角形上。
function PointInTriangle(x0, y0, x1, y1, x2, y2, x3, y3) {
  var divisor = (y2 - y3)*(x1 - x3) + (x3 - x2)*(y1 - y3);
  var a = ((y2 - y3)*(x0 - x3) + (x3 - x2)*(y0 - y3)) / divisor;
  var b = ((y3 - y1)*(x0 - x3) + (x1 - x3)*(y0 - y3)) / divisor;
  var c = 1 - a - b;

  return a >= 0 && a <= 1 && b >= 0 && b <= 1 && c >= 0 && c <= 1;
}

//通过坐标点返回点击的类型
function GetTipBtnType(tag, x0, y0) {
  for (var i = 0; i < ZW_PicArray.length; i++) {
    if(ZW_PicArray[i].tag.indexOf(tag+'-') > -1)
    {
      var posX = ZW_PicArray[i].x0;
      var posY = ZW_PicArray[i].y0;
      var width = ZW_PicArray[i].width*ZW_PicArray[i].scale0;
      var height = ZW_PicArray[i].height*ZW_PicArray[i].scale0;
      var pDis = Math.sqrt((width * width) + (height * height));

      var rad1 = Math.asin(height/pDis)+Math.PI*ZW_PicArray[i].rot0/180.0;

      var posX1 = posX-0.5*pDis*Math.cos(rad1);
      var posY1 = posY-0.5*pDis*Math.sin(rad1);

      var rad2 = (Math.PI -Math.asin(height/pDis))+Math.PI*ZW_PicArray[i].rot0/180.0;
      
      var posX2 = posX-0.5*pDis*Math.cos(rad2);
      var posY2 = posY-0.5*pDis*Math.sin(rad2);

      var rad3 = (Math.PI +Math.asin(height/pDis))+Math.PI*ZW_PicArray[i].rot0/180.0;
      
      var posX3 = posX-0.5*pDis*Math.cos(rad3);
      var posY3 = posY-0.5*pDis*Math.sin(rad3);

      var rad4 = (2*Math.PI -Math.asin(height/pDis))+Math.PI*ZW_PicArray[i].rot0/180.0;
      
      var posX4 = posX-0.5*pDis*Math.cos(rad4);
      var posY4 = posY-0.5*pDis*Math.sin(rad4);


      var pDis1 = Math.sqrt(((x0-posX1) * (x0-posX1)) + ((y0-posY1) * (y0-posY1)));
      var pDis2 = Math.sqrt(((x0-posX2) * (x0-posX2)) + ((y0-posY2) * (y0-posY2)));
      var pDis3 = Math.sqrt(((x0-posX3) * (x0-posX3)) + ((y0-posY3) * (y0-posY3)));

      if(pDis1 < 30)
      {
        return 'remove';
      }else if(pDis2 < 30){
        return 'scale';
      }else if(pDis3 < 30){
        return 'rotation';
      }else{
        return '';
      }

      //return [{x:posX1,y:posY1},{x:posX2,y:posY2},{x:posX3,y:posY3},{x:posX4,y:posY4}];
    }
  }
}

//通过点的位置来获取贴纸的tag值
function GetTipTagByPoint(point) {
  for (var i = 0; i < ZW_PicArray.length; i++) {
    for(var j=ZW_tipArray.length-1; j>-1; j--){
      if(ZW_PicArray[i].tag == ZW_tipArray[j]+'-0'){
        var posX = ZW_PicArray[i].x0;
        var posY = ZW_PicArray[i].y0;
        var width = ZW_PicArray[i].width * ZW_PicArray[i].scale0;
        var height = ZW_PicArray[i].height * ZW_PicArray[i].scale0;
        var pDis = Math.sqrt((width * width) + (height * height));

        var rad1 = Math.asin(height / pDis) + Math.PI * ZW_PicArray[i].rot0 / 180.0;

        var posX1 = posX - 0.5 * pDis * Math.cos(rad1);
        var posY1 = posY - 0.5 * pDis * Math.sin(rad1);

        var rad2 = (Math.PI - Math.asin(height / pDis)) + Math.PI * ZW_PicArray[i].rot0 / 180.0;

        var posX2 = posX - 0.5 * pDis * Math.cos(rad2);
        var posY2 = posY - 0.5 * pDis * Math.sin(rad2);

        var rad3 = (Math.PI + Math.asin(height / pDis)) + Math.PI * ZW_PicArray[i].rot0 / 180.0;

        var posX3 = posX - 0.5 * pDis * Math.cos(rad3);
        var posY3 = posY - 0.5 * pDis * Math.sin(rad3);

        var rad4 = (2 * Math.PI - Math.asin(height / pDis)) + Math.PI * ZW_PicArray[i].rot0 / 180.0;

        var posX4 = posX - 0.5 * pDis * Math.cos(rad4);
        var posY4 = posY - 0.5 * pDis * Math.sin(rad4);

        var pDis1 = Math.sqrt(((point.x-posX1) * (point.x-posX1)) + ((point.y-posY1) * (point.y-posY1)));
        var pDis2 = Math.sqrt(((point.x-posX2) * (point.x-posX2)) + ((point.y-posY2) * (point.y-posY2)));
        var pDis3 = Math.sqrt(((point.x-posX3) * (point.x-posX3)) + ((point.y-posY3) * (point.y-posY3)));


        var result = PointInTriangle(point.x,point.y,posX1,posY1,posX2,posY2,posX3,posY3) || 
          PointInTriangle(point.x,point.y,posX1,posY1,posX3,posY3,posX4,posY4) || pDis1 < 30 || pDis2 < 30 || pDis3 < 30;
        
        if(result) return ZW_tipArray[j];
      }
    }
  }
  return '';
  // for (var i = 0; i < ZW_PicArray.length; i++) {
  //   if(ZW_PicArray[i].tag.indexOf('tip'+index) > -1)
  //   {
  //     var posX = ZW_PicArray[i].x0;
  //     var posY = ZW_PicArray[i].y0;
  //     var width = ZW_PicArray[i].width*ZW_PicArray[i].scale0;
  //     var height = ZW_PicArray[i].height*ZW_PicArray[i].scale0;
  //     var pDis = Math.sqrt((width * width) + (height * height));

  //     var rad1 = Math.asin(height/pDis)+Math.PI*ZW_PicArray[i].rot0/180.0;

  //     var posX1 = posX-0.5*pDis*Math.cos(rad1);
  //     var posY1 = posY-0.5*pDis*Math.sin(rad1);

  //     var rad2 = (Math.PI -Math.asin(height/pDis))+Math.PI*ZW_PicArray[i].rot0/180.0;
      
  //     var posX2 = posX-0.5*pDis*Math.cos(rad2);
  //     var posY2 = posY-0.5*pDis*Math.sin(rad2);

  //     var rad3 = (Math.PI +Math.asin(height/pDis))+Math.PI*ZW_PicArray[i].rot0/180.0;
      
  //     var posX3 = posX-0.5*pDis*Math.cos(rad3);
  //     var posY3 = posY-0.5*pDis*Math.sin(rad3);

  //     var rad4 = (2*Math.PI -Math.asin(height/pDis))+Math.PI*ZW_PicArray[i].rot0/180.0;
      
  //     var posX4 = posX-0.5*pDis*Math.cos(rad4);
  //     var posY4 = posY-0.5*pDis*Math.sin(rad4);

  //     return [{x:posX1,y:posY1},{x:posX2,y:posY2},{x:posX3,y:posY3},{x:posX4,y:posY4}];
  //   }
  // }
}

//获取贴纸的四个点的位置
function GetTipPoints(tag) {
  for (var i = 0; i < ZW_PicArray.length; i++) {
    if(ZW_PicArray[i].tag.indexOf(tag+'-') > -1)
    {
      var posX = ZW_PicArray[i].x0;
      var posY = ZW_PicArray[i].y0;
      var width = ZW_PicArray[i].width*ZW_PicArray[i].scale0;
      var height = ZW_PicArray[i].height*ZW_PicArray[i].scale0;
      var pDis = Math.sqrt((width * width) + (height * height));

      var rad1 = Math.asin(height/pDis)+Math.PI*ZW_PicArray[i].rot0/180.0;

      var posX1 = posX-0.5*pDis*Math.cos(rad1);
      var posY1 = posY-0.5*pDis*Math.sin(rad1);

      var rad2 = (Math.PI -Math.asin(height/pDis))+Math.PI*ZW_PicArray[i].rot0/180.0;
      
      var posX2 = posX-0.5*pDis*Math.cos(rad2);
      var posY2 = posY-0.5*pDis*Math.sin(rad2);

      var rad3 = (Math.PI +Math.asin(height/pDis))+Math.PI*ZW_PicArray[i].rot0/180.0;
      
      var posX3 = posX-0.5*pDis*Math.cos(rad3);
      var posY3 = posY-0.5*pDis*Math.sin(rad3);

      var rad4 = (2*Math.PI -Math.asin(height/pDis))+Math.PI*ZW_PicArray[i].rot0/180.0;
      
      var posX4 = posX-0.5*pDis*Math.cos(rad4);
      var posY4 = posY-0.5*pDis*Math.sin(rad4);

      return [{x:posX1,y:posY1},{x:posX2,y:posY2},{x:posX3,y:posY3},{x:posX4,y:posY4}];
    }
  }
}


//设置场景中的贴纸是否编辑
function SetEditState(bEdit){
  ZW_bTipEdit = bEdit;
}

//获取贴纸的位置
function GetTipPosition(tag) {
  for (var i = 0; i < ZW_PicArray.length; i++) {
    if(ZW_PicArray[i].tag.indexOf(tag+'-') > -1)
    {
      return [ZW_PicArray[i].x0,ZW_PicArray[i].y0];
    }
  }
}

//设置贴纸的位置
function SetTipPosition(tag, x, y) {
  for (var i = 0; i < ZW_PicArray.length; i++) {
    if(ZW_PicArray[i].tag.indexOf(tag+'-') > -1)
    {
      ZW_PicArray[i].x0 = x;
      ZW_PicArray[i].y0 = y;
    }
  }
}

//获取贴纸的旋转值
function GetTipRotation(tag) {
  for (var i = 0; i < ZW_PicArray.length; i++) {
    if(ZW_PicArray[i].tag.indexOf(tag+'-') > -1)
    {
      return ZW_PicArray[i].rot0;
    }
  }
}

//设置贴纸的旋转值
function SetTipRotation(tag, rot) {
  for (var i = 0; i < ZW_PicArray.length; i++) {
    if(ZW_PicArray[i].tag.indexOf(tag+'-') > -1)
    {
      ZW_PicArray[i].rot0 = rot;
      
    }
  }
}

//获取贴纸的缩放
function GetTipScale(tag) {
  for (var i = 0; i < ZW_PicArray.length; i++) {
    if(ZW_PicArray[i].tag.indexOf(tag+'-') > -1)
    {
      return ZW_PicArray[i].scale0;
    }
  }
}

//设置贴纸的缩放
function SetTipScale(tag, scale) {
  for (var i = 0; i < ZW_PicArray.length; i++) {
    if(ZW_PicArray[i].tag.indexOf(tag+'-') > -1)
    {
      ZW_PicArray[i].scale0 = scale;
      
    }
  }
}


//删除贴纸
function RemoveTip(tag) {
  for (var i = 0; i < ZW_PicArray.length; i++) {
    if(ZW_PicArray[i].tag.indexOf(tag+'-') > -1)
    {
      ZW_PicArray.splice(i,1);
      i--;
    }
  }

  for (var i = 0; i < ZW_tipArray.length; i++) {
    if(ZW_tipArray[i].indexOf(tag) > -1)
    {
      ZW_tipArray.splice(i,1);
      ZW_tipNumArray.splice(i,1);
      i--;
    }
  }
}

function AddTip(tag,url,num,width,height,x,y,rot,scale) {
  if(!x) x = 0;
  if(!y) y = 0;
  //var gifUrlArray = [];
  ZW_tipArray.push(tag);
  ZW_tipNumArray.push(num);
      for(var i=0; i<num; i++)
      {
      //  gifUrlArray.push(url +'-'+ i + '.png');

        var rUrl = url +'-'+ i + '.png';
        console.log(rUrl);
        AddGifImage2(tag, rUrl, x, y, width * 0.3 , height * 0.3, rot, scale,true,num,9,i);
        
      }
}

function AddGifImage2(tag, url, x, y, width, height, rot, scale,visible,num1,order,index) {
  let contentPicNetPath = url;

  //var cImage = { tag: tag, x: x, y: y, width: width, height: height, url: '', rot: rot, scale: scale ,visible:visible,renderOrder:order,type:'tip'};

  var cImage = { tag: tag+'-'+index, x: 0, y: 0, x0: x, y0: y, width: width, height: height, url: '', rot: 0, rot0: rot,scale: scale, scale0: 1.0,visible:visible,renderOrder:order,type:'tip'};

  ZW_PicArray.push(cImage);

  jd.getImageInfo({
    //保存网络图片
    src: contentPicNetPath,
    success:
      function (res) {
        cImage.url = res.path;

        for(var i=0; i<ZW_tipArray.length; i++)
        {
          if(ZW_tipArray[i] == tag){
            ZW_tipNumArray[i] = ZW_tipNumArray[i]-1;

            if(ZW_tipNumArray[i] == 0)
            {
              console.log('gif is over');
              var coords = { x: 0 };
              new TWEEN.Tween(coords)
                .to({ x: num1 }, 1000 * num1 / 4.0)
                .repeat(Infinity)
                .easing(TWEEN.Easing.Linear.None)
                .onUpdate(function () {
                  var num = Math.round(coords.x) % num1;

                  for (var j = 0; j < num1; j++) {
                    SetImageVisible(tag + '-' + j, false);
                  }

                  SetImageVisible(tag + '-' + num, true);

                })
                .delay(0)
                .start();
            }
          }
        }
      }
  })
}


function AddBackgroud(type) {

    RemoveImage('back1');

  if(type == 1){
    AddGifImage('back1', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/bg1.jpg', ZW_Info.windowWidth*0.5 , ZW_Info.windowWidth *0.5* 2.16533,
      ZW_Info.windowWidth , ZW_Info.windowWidth * 2.16533, 0, 1.0,false,0);
    
      that.setData({
        backGifUrl:'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif1/bg1.gif',
        backBottomUrl:'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif1/BG1-bottom.jpg'
      });
  }else if(type == 2){
    AddGifImage('back1', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif1/bg2.jpg', ZW_Info.windowWidth*0.5 , ZW_Info.windowWidth *0.5* 2.16533,
      ZW_Info.windowWidth , ZW_Info.windowWidth * 2.16533, 0, 1.0,false,0);
    
      that.setData({
        backGifUrl:'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif1/bg2.gif',
        backBottomUrl:'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif1/BG2-bottom.jpg'
      });
  }else if(type == 3){
    AddGifImage('back1', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif1/bg3.jpg', ZW_Info.windowWidth*0.5 , ZW_Info.windowWidth *0.5* 2.16533,
      ZW_Info.windowWidth , ZW_Info.windowWidth * 2.16533, 0, 1.0,false,0);

      that.setData({
        backGifUrl:'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif1/bg3.gif',
        backBottomUrl:'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif1/BG3-bottom.jpg'
      });

  }else if(type == 4){
    AddGifImage('back1', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif1/bg4.jpg', ZW_Info.windowWidth*0.5 , ZW_Info.windowWidth *0.5* 2.16533,
      ZW_Info.windowWidth , ZW_Info.windowWidth * 2.16533, 0, 1.0,false,0);
      
      that.setData({
        backGifUrl:'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif1/bg4.gif',
        backBottomUrl:'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif1/BG4-bottom.jpg'
      });
  }else if(type == 5){
    AddGifImage('back1', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif1/bg5.jpg', ZW_Info.windowWidth*0.5 , ZW_Info.windowWidth *0.5* 2.16533,
      ZW_Info.windowWidth , ZW_Info.windowWidth * 2.16533, 0, 1.0,false,0);
      
      that.setData({
        backGifUrl:'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif1/bg5.gif',
        backBottomUrl:'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif1/BG5-bottom.jpg'
      });
  }
}

function AddGifImage(tag, url, x, y, width, height, rot, scale,visible,order) {
  let contentPicNetPath = url;

  var cImage = { tag: tag, x: x, y: y, width: width, height: height, url: '', rot: rot, scale: scale ,visible:visible,renderOrder:order};
  ZW_PicArray.unshift(cImage);


  jd.getImageInfo({
    //保存网络图片
    src: contentPicNetPath,
    success:
      function (res) {
        cImage.url = res.path;
      }
  })
}

var Load_URL_Num= 0;
var Load_URL_Num0= 0;

var G_Count = 0;

function AddGifImage1(tag, url, x, y, width, height, rot, scale,visible,order) {
  let contentPicNetPath = url;

  var cImage = { tag: tag, x: x, y: y, width: width, height: height, url: '', rot: rot, scale: scale ,visible:visible,renderOrder:order};
  ZW_PicArray.push(cImage);

  Load_URL_Num0++;

  jd.getImageInfo({
    //保存网络图片
    src: contentPicNetPath,
    success:
      function (res) {
        cImage.url = res.path;

        Load_URL_Num0--;
        if(Load_URL_Num0 == 0){
          
            var coords = { x: 4 };
            new TWEEN.Tween(coords)
              .to({ x: 0 }, 1000 * 5.0 / 15.0)
              .repeat(Infinity).repeatDelay(1200)
              .easing(TWEEN.Easing.Linear.None)
              .onUpdate(function () {
                var num = Math.round(coords.x) % 5;
                
                for (var i = 4; i > -1; i--) {
                  SetImageVisible('expression-' + i, false);
                }

                SetImageVisible('expression-' + num, true);
              })
              .delay(0)
              .start();
        }
      }
  })
}

function RemoveImage(tag) {
  for (var i = 0; i < ZW_PicArray.length; i++) {
    if (tag == ZW_PicArray[i].tag) {
      ZW_PicArray.splice(i,1);
      i--;
    }
  }
}


function SetImageVisible(tag, visible) {
  for (var i = 0; i < ZW_PicArray.length; i++) {
    if (tag == ZW_PicArray[i].tag) {
      ZW_PicArray[i].visible = visible;
    }
  }
}


function SetImageScale(tag, scale) {
  for (var i = 0; i < ZW_PicArray.length; i++) {
    if (tag == ZW_PicArray[i].tag) {
      ZW_PicArray[i].scale = scale;
    }
  }
}

function SetImageUrl(tag, url) {
  let contentPicNetPath = url;

  // var cImage = {tag:tag,x:x,y:y,width:width,height:height,url:'',rot:rot,scale:scale};
  // ZW_PicArray.push(cImage);

  if (url == '') {    //不显示某些部件
    for (var i = 0; i < ZW_PicArray.length; i++) {
      if (tag == ZW_PicArray[i].tag) {
        ZW_PicArray[i].url = '';
      }
    }
  }

  jd.getImageInfo({
    //保存网络图片
    src: contentPicNetPath,
    success:
      function (res) {
        for (var i = 0; i < ZW_PicArray.length; i++) {
          if (tag == ZW_PicArray[i].tag) {
            ZW_PicArray[i].url = res.path;

            // jd.showToast({
            //   title: '替换',
            //   icon: 'success',
            //   duration: 1500
            // })
          }
        }
        //cImage.url = res.path;
        // ZW_PicArray.push({tag:tag,x:x,y:y,width:width,height:height,url:res.path,rot:rot,scale:scale});
        //console.log(ZW_PicArray);

      }
  })
}

//松开手指的处理
function ReleaseProcess() {

}

//改变视角
function ChangeView(type) {
  if(type == 'top')
  {
    var coords = { x: Scene_OffsetX, y: Scene_OffsetY, x1: Scene_ScaleX, y1: Scene_ScaleY };
    new TWEEN.Tween(coords)
      .to({ x: -ZW_Info.windowWidth * 0.08, y: +ZW_Info.windowWidth * 0.1, x1: 1.2, y1: 1.2 }, 500)
      .repeat(0)
      .easing(TWEEN.Easing.Linear.None)
      .onUpdate(function () {
        Scene_OffsetX = coords.x;
        Scene_OffsetY = coords.y;
        Scene_ScaleX = coords.x1;
        Scene_ScaleY = coords.y1;
      })
      .delay(0)
      .start();
  }else if(type == 'middle')
  {
    var coords = { x: Scene_OffsetX, y: Scene_OffsetY, x1: Scene_ScaleX, y1: Scene_ScaleY };
    new TWEEN.Tween(coords)
      .to({ x: 0, y: 0, x1: 1.0, y1: 1.0 }, 500)
      .repeat(0)
      .easing(TWEEN.Easing.Linear.None)
      .onUpdate(function () {
        Scene_OffsetX = coords.x;
        Scene_OffsetY = coords.y;
        Scene_ScaleX = coords.x1;
        Scene_ScaleY = coords.y1;
      })
      .delay(0)
      .start();
  }else if(type == 'bottom')
  {
    var coords = { x: Scene_OffsetX, y: Scene_OffsetY, x1: Scene_ScaleX, y1: Scene_ScaleY };
    new TWEEN.Tween(coords)
      .to({ x: -ZW_Info.windowWidth * 0.08, y: -ZW_Info.windowWidth * 0.4, x1: 1.2, y1: 1.2 }, 500)
      .repeat(0)
      .easing(TWEEN.Easing.Linear.None)
      .onUpdate(function () {
        Scene_OffsetX = coords.x;
        Scene_OffsetY = coords.y;
        Scene_ScaleX = coords.x1;
        Scene_ScaleY = coords.y1;
      })
      .delay(0)
      .start();
  }
}


//距离计算公式
function M_distance(a, b) {
  var x = b[0] - a[0],
    y = b[1] - a[1],
    z = b[2] - a[2];
  return Math.sqrt(x * x + y * y + z * z);
};

//角度计算公式
function M_GetAngle(v0, v1, v2) {
  var linev0v1 = M_distance(v0, v1);
  var linev0v2 = M_distance(v0, v2);
  var linev1v2 = M_distance(v1, v2);

  return Math.acos((linev0v1 * linev0v1 + linev1v2 * linev1v2 - linev0v2 * linev0v2) / (2 * linev0v1 * linev1v2)) * 180.0 / 3.1415926;
}

function StartDecorationAnimation(tag, index1, index2, time) {
  //console.log(index1+'++++'+index2);
  if (!time) time = 300;
  var coords = { x: index1 };
  new TWEEN.Tween(coords)
    .to({ x: index2 }, time)
    .repeat(0)
    .easing(TWEEN.Easing.Linear.None)
    .onUpdate(function () {

    })
    .delay(0)
    .start();
}

function GetNodeByTag(tag) {
  for (var i = 0; i < ZW_PicArray.length; i++) {
    if (ZW_PicArray[i].tag == tag) {
      return ZW_PicArray[i];
    }
  }
  return null;
}

//获取椭圆的轨迹点
function GetEllipsePoints(data) {
  var points = [];

  for (var d = 0; d <= data.t; d++) {
          var point = {x:0,y:0};
          var twoPi = Math.PI * 2;
          var deltaAngle = data.aEndAngle - data.aStartAngle;
          var samePoints = Math.abs(deltaAngle) < Number.EPSILON; // ensures that deltaAngle is 0 .. 2 PI

          while (deltaAngle < 0) {
                  deltaAngle += twoPi;
          }

          while (deltaAngle > twoPi) {
                  deltaAngle -= twoPi;
          }

          if (deltaAngle < Number.EPSILON) {
                  if (samePoints) {
                          deltaAngle = 0;
                  } else {
                          deltaAngle = twoPi;
                  }
          }

          if (data.aClockwise === true && !samePoints) {
                  if (deltaAngle === twoPi) {
                          deltaAngle = -twoPi;
                  } else {
                          deltaAngle = deltaAngle - twoPi;
                  }
          }

          var angle = data.aStartAngle + d / data.t * deltaAngle;
          var x = data.aX + data.xRadius * Math.cos(angle);
          var y = data.aY + data.yRadius * Math.sin(angle);

          if (data.aRotation !== 0) {
                  var cos = Math.cos(data.aRotation);
                  var sin = Math.sin(data.aRotation);
                  var tx = x - data.aX;
                  var ty = y - data.aY; // Rotate the point about the center of the ellipse.

                  x = tx * cos - ty * sin + data.aX;
                  y = tx * sin + ty * cos + data.aY;
          }

          point.x = x;
          point.y = y; //return point.set(x, y);

          points.push(point);
  }

  return points;
}

function ShowProcess() {
  ZW_PicArray.sort(function (a, b) { return a.renderOrder - b.renderOrder });

  ZW_Ctx.scale(Scene_ScaleX, Scene_ScaleY);
  for (var i = 0; i < ZW_PicArray.length; i++) {
    if (ZW_PicArray[i].url != '' && ZW_PicArray[i].visible) {

      if (ZW_PicArray[i].type == 'tip') {
        let contentPicLocalPath = ZW_PicArray[i].url;

        ZW_Ctx.translate(ZW_PicArray[i].x0 + Scene_OffsetX, ZW_PicArray[i].y0 + Scene_OffsetY);
        ZW_Ctx.rotate(ZW_PicArray[i].rot0 * Math.PI / 180);
        ZW_Ctx.scale(ZW_PicArray[i].scale0, ZW_PicArray[i].scale0);

        ZW_Ctx.translate(ZW_PicArray[i].x, ZW_PicArray[i].y);
        ZW_Ctx.drawImage(contentPicLocalPath, -ZW_PicArray[i].scale * ZW_PicArray[i].width / 2,
          -ZW_PicArray[i].scale * ZW_PicArray[i].height / 2, ZW_PicArray[i].width * ZW_PicArray[i].scale, ZW_PicArray[i].height * ZW_PicArray[i].scale);

        if (ZW_bTipEdit && ZW_PicArray[i].tag.indexOf(Current_Tip_Tag+'-') == 0) {  //只有选中的贴纸才显示边框和按钮
          //贴纸画框
          ZW_Ctx.setStrokeStyle("#ffffff");
          ZW_Ctx.setLineWidth(2.0 / ZW_PicArray[i].scale0);
          ZW_Ctx.rect(-ZW_PicArray[i].scale * ZW_PicArray[i].width / 2,
            -ZW_PicArray[i].scale * ZW_PicArray[i].height / 2, ZW_PicArray[i].width * ZW_PicArray[i].scale, ZW_PicArray[i].height * ZW_PicArray[i].scale);
          ZW_Ctx.stroke();

          //画删除按钮
          ZW_Ctx.drawImage(ZW_Close_Url, -ZW_PicArray[i].scale * ZW_PicArray[i].width / 2 - 12.0 / ZW_PicArray[i].scale0,
            -ZW_PicArray[i].scale * ZW_PicArray[i].height / 2 - 12.0 / ZW_PicArray[i].scale0, 24.0 / ZW_PicArray[i].scale0, 24.0 / ZW_PicArray[i].scale0);

          //画缩放按钮
          ZW_Ctx.drawImage(ZW_Scale_Url, ZW_PicArray[i].width * ZW_PicArray[i].scale - ZW_PicArray[i].scale * ZW_PicArray[i].width / 2 - 12.0 / ZW_PicArray[i].scale0,
            -ZW_PicArray[i].scale * ZW_PicArray[i].height / 2 - 12.0 / ZW_PicArray[i].scale0, 24.0 / ZW_PicArray[i].scale0, 24.0 / ZW_PicArray[i].scale0);

          //画旋转按钮
          ZW_Ctx.drawImage(ZW_Rot_Url, ZW_PicArray[i].width * ZW_PicArray[i].scale - ZW_PicArray[i].scale * ZW_PicArray[i].width / 2 - 12.0 / ZW_PicArray[i].scale0,
            ZW_PicArray[i].height * ZW_PicArray[i].scale - ZW_PicArray[i].scale * ZW_PicArray[i].height / 2 - 12.0 / ZW_PicArray[i].scale0, 24.0 / ZW_PicArray[i].scale0, 24.0 / ZW_PicArray[i].scale0);
        }

        ZW_Ctx.translate(-ZW_PicArray[i].x, -ZW_PicArray[i].y);

        ZW_Ctx.scale(1.0 / ZW_PicArray[i].scale0, 1.0 / ZW_PicArray[i].scale0);
        ZW_Ctx.rotate(-ZW_PicArray[i].rot0 * Math.PI / 180);
        ZW_Ctx.translate(-ZW_PicArray[i].x0 - Scene_OffsetX, -ZW_PicArray[i].y0 - Scene_OffsetY);
      } else {
        let contentPicLocalPath = ZW_PicArray[i].url;

        ZW_Ctx.translate(ZW_PicArray[i].x + Scene_OffsetX, ZW_PicArray[i].y + Scene_OffsetY);
        ZW_Ctx.rotate(ZW_PicArray[i].rot * Math.PI / 180);
        ZW_Ctx.drawImage(contentPicLocalPath, -ZW_PicArray[i].scale * ZW_PicArray[i].width / 2,
          -ZW_PicArray[i].scale * ZW_PicArray[i].height / 2, ZW_PicArray[i].width * ZW_PicArray[i].scale, ZW_PicArray[i].height * ZW_PicArray[i].scale);
        ZW_Ctx.rotate(-ZW_PicArray[i].rot * Math.PI / 180);
        ZW_Ctx.translate(-ZW_PicArray[i].x - Scene_OffsetX, -ZW_PicArray[i].y - Scene_OffsetY);
      }
    }

  }
  ZW_Ctx.draw();
}

function animation() {
  var times = 0
  var i = setInterval(function () {

    // //console.log('sss');
    // gStage.update();
    //angle++;
    // let contentPicLocalPath = "/img/t1.png"
    // ZW_Ctx.translate(20, 50);
    // ZW_Ctx.rotate(angle * Math.PI / 180);
    // ZW_Ctx.drawImage(contentPicLocalPath, -20, -50, 40, 100)

    // ZW_Ctx.translate(200, 200);
    // ZW_Ctx.rotate(-angle * Math.PI / 180);
    // ZW_Ctx.drawImage(contentPicLocalPath, -50, -50, 100, 100)

    TWEEN.update();

    ShowProcess();
  }, 20)
}


var Current_Tip_Tag = '';

var Current_Tip_Rot = 0;

var Current_Back_Index = 1;

var testIndex = 1;

Page({
  data: {
    listerListInfo: [],
    leftValue1: 0,
    canvasWidth: 360,
    canvasHeight: 360,
    backGifUrl:'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif1/bg2.gif',
    backBottomUrl:'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif1/BG2-bottom.jpg'
  },
  onReady: function () {
    that = this;
    this.position = {
      x: 150,
      y: 150,
      vx: 2,
      vy: 2
    };
    ZW_Ctx = jd.createCanvasContext('canvas')

    ZW_Info = jd.getSystemInfoSync();
    console.log(ZW_Info);
    gIsIphone = ZW_Info.platform == "ios";

    ZW_PixelRatio = ZW_Info.pixelRatio;

    // this.setData({
    //   leftValue: (info.windowWidth-300)/2
    // });

    this.setData({
      canvasWidth: ZW_Info.windowWidth,
      canvasHeight: ZW_Info.windowHeight
    });

    // jd.showToast({
    //   title: ZW_Info.windowWidth.toString()+','+ZW_Info.windowHeight.toString(),
    //   icon: 'success',
    //   duration: 1500
    // })

    //AddChain('chain0','https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/lamer-jd/test1.jpg');
    //AddChain('chain0','https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/aj36/20220121/touming_B.png');
    // AddImage('back', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/test/vans/BG1.png', ZW_Info.windowWidth*0.5 , ZW_Info.windowWidth *0.5* 1.026667,
    //   ZW_Info.windowWidth , ZW_Info.windowWidth * 1.026667, 0, 1.0);
      AddBackgroud(5);

      AddImage('hair', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/hair/hair03.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
      ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0,true,1);

    AddImage('body', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/body/Body01.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
      ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0,true,2);


    AddImage('bang', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/bangs/bangs03.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
      ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0,true,3);


    // AddExpression('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/expression/i/Male01');

     AddExpression('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/expression/i/IWD08');


    //  AddImage('exr', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/expression/i/Male01-0.png', ZW_Info.windowWidth * 0.526, ZW_Info.windowWidth * 0.462,
    //   ZW_Info.windowWidth * 0.5333*0.55, ZW_Info.windowWidth * 0.42666*0.55, 0, 1.0,true,20);


    // AddImage('expression', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/expression/expression01.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
    //   ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0,true);

    //   AddImage('expression1', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/expression/ex2/female02-0.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
    //   ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0,true);


    AddImage('shoe', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/shoes/shoes01.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
      ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0,true,5);

    AddImage('pant', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/pants/pants01.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
      ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0,true,6);

    AddImage('cloth', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/jacket/jacket02.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
      ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0,true,7);

    AddImage('hat', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/hat/hat01.png', ZW_Info.windowWidth * 0.5, ZW_Info.windowWidth * 0.85,
      ZW_Info.windowWidth * 0.5 * 1.4, ZW_Info.windowWidth * 1.4, 0, 1.0,true,8);

      
      //var tipTag = guid();
      //Current_Tip_Tag = tipTag;
      //AddTip(Current_Tip_Tag,'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/1',6,629,390,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.5);
      
      // AddTip(guid(),'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/2',4,480,452,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.6);
      // AddTip(guid(),'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/3',5,460,480,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.7);
      // AddTip(guid(),'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/4',6,480,248,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.8);
      // AddTip(guid(),'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/5',3,480,347,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.9);
      // AddTip(guid(),'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/6',3,387,480,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*1.0);
      // AddTip(guid(),'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/7',3,402,480,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*1.1);
      // AddTip(guid(),'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/8',4,480,480,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*1.2);

     
      //AddTip('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/',1,6,629,390,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.5);
    
    //AddTip('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/',2,4,480,452,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.75);
  // AddTip('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/',3,5,460,480,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.5);
    //AddTip('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/',4,6,480,248,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.5);
    //AddTip('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/',5,3,480,347,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.5);
    //AddTip('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/',6,3,387,480,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.5);
    //AddTip('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/',7,3,402,480,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.5);
    //AddTip('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/',8,4,480,480,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.5);

    // ZW_Ctx.setStrokeStyle("#00ff00")
    // ZW_Ctx.setLineWidth(5)
    // ZW_Ctx.rect(0, 0, 300, 300)
    // ZW_Ctx.stroke()

    animation();

    // context.setStrokeStyle("#ff0000")
    // context.setLineWidth(2)
    // context.moveTo(160, 100)
    // context.arc(100, 100, 60, 0, 2 * Math.PI, true)
    // context.moveTo(140, 100)
    // context.arc(100, 100, 40, 0, Math.PI, false)
    // context.moveTo(85, 80)
    // context.arc(80, 80, 5, 0, 2 * Math.PI, true)
    // context.moveTo(125, 80)
    // context.arc(120, 80, 5, 0, 2 * Math.PI, true)
    // context.stroke()

    // 网络
    // let contentPicNetPath = "https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/jd_starbucks/remove.png"
    // jd.getImageInfo({
    //   //保存网络图片
    //   src:contentPicNetPath,
    //   success:
    //     function (res) {
    //       //请求成功后将会生成一个本地路径即res.path,然后将该路径缓存到storageKeyUrl关键字中 
    //       // this.globalData.pic = res.path;
    //       console.log("jd.getImageInfo --- ok")
    //       console.log(res)

    //       context.moveTo(0, 0);       //设置起点状态
    //       context.lineTo(700, 700);       //设置末端状态
    //       context.lineWidth = 5;          //设置线宽状态
    //       context.strokeStyle = "#ff00ff";  //设置线的颜色状态
    //       context.stroke();               //进行绘制


    //       context.drawImage(res.path, 100, 100, 100, 100)
    //        context.draw() // 如果不加这句  就画不出remove；如果加上这句，remove会出来，但之前画的内容就没了

    //     }
    // })



  },
  btnclick: function () {
    SetImageUrl('cloth', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/jacket/jacket01.png');
  },
  btnclick1: function () {
    SetImageUrl('pant', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/pants/pants02.png');
  },
  btnclick2: function () {
    SetImageUrl('hat', '');
  },
  btnclick3: function () {
    SetImageUrl('bang', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/bangs/bangs01.png');
    SetImageUrl('hair', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/hair/hair01.png');
    //this.onGetShot();
  },
  btnclick4: function () {
    SetImageUrl('body', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/body/Body02.png');
  },
  btnclick5: function () {
    AddExpression('https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/expression/i/Male02');
  },
  btnclick6: function () {
    var index = Math.round(Math.random()*3); 
    if(Current_Back_Index == 1)
    {
      Current_Back_Index = 2;
    }else if(Current_Back_Index == 2)
    {
      Current_Back_Index = 3;
    }else if(Current_Back_Index == 3)
    {
      Current_Back_Index = 4;
    }else if(Current_Back_Index == 4)
    {
      Current_Back_Index = 1;
    }
      
    AddBackgroud(Current_Back_Index);
  },
  btnclick7: function () {
    this.onGetShot();
  },
  btnclick8: function () {
    var tipTag = guid();
      Current_Tip_Tag = tipTag;
      
      if(testIndex == 1)
      {
        AddTip(Current_Tip_Tag,'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/1',6,629,390,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.5,0,1.0);
      }else if(testIndex == 2)
      {
        AddTip(Current_Tip_Tag,'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/2',4,480,452,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.6,0,1.0);
      }else if(testIndex == 3)
      {
        AddTip(Current_Tip_Tag,'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/3',5,460,480,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.7,0,1.0);
      }else if(testIndex == 4)
      {
        AddTip(Current_Tip_Tag,'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/4',6,480,248,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.8,0,1.0);
      }else if(testIndex == 5)
      {
        AddTip(Current_Tip_Tag,'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/5',3,480,347,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*0.9,0,1.0);
      }else if(testIndex == 6)
      {
        AddTip(Current_Tip_Tag,'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/6',3,387,480,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*1.0,0,1.0);
      }else if(testIndex == 7)
      {
        AddTip(Current_Tip_Tag,'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/7',3,402,480,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*1.1,0,1.0);
      }else if(testIndex == 8)
      {
        AddTip(Current_Tip_Tag,'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/gif/tips/8',4,480,480,ZW_Info.windowWidth*0.5,ZW_Info.windowWidth*1.2,0,1.0);
      }
      testIndex++; 

      if(testIndex > 8) testIndex =1;
      
    //SetEditState(!ZW_bTipEdit);
  },
  btnclick9: function () {
    
  },
  btnclick10: function () {
    
  },
  btnclick11: function () {
    
  },
  btnclick12: function () {
    
  },
  onUnload: function () {
    clearInterval(this.interval);
  },
  onTouchStart(e) {
    //console.log(e);
    if (gIsIphone) {
      // ZW_PixelRatio = 1;
      // e.touches[0].y -= ZW_Info.screenHeight - ZW_Info.windowHeight;
    }
    var clickPos = { x: e.touches[0].x, y: e.touches[0].y };

    ZW_ClickPos = { x: e.touches[0].x, y: e.touches[0].y };

    // jd.showToast({
    //   title: e.touches[0].y.toString() +','+ e.touches[0].y.toString(),
    //   icon: 'success',
    //   duration: 1500
    // })
    var cTag = GetTipTagByPoint(ZW_ClickPos);
    

    Current_Tip_Tag = cTag;
    var points = GetTipPoints(Current_Tip_Tag);

    var result = PointInTriangle(ZW_ClickPos.x,ZW_ClickPos.y,points[0].x,points[0].y,points[1].x,points[1].y,points[2].x,points[2].y) || 
      PointInTriangle(ZW_ClickPos.x,ZW_ClickPos.y,points[0].x,points[0].y,points[2].x,points[2].y,points[3].x,points[3].y);

      var type = GetTipBtnType(Current_Tip_Tag,ZW_ClickPos.x,ZW_ClickPos.y);

      if(type == '')
      {
        if (result) {
          ZW_MoveType = 'position';
        }
      }else
      {
        if(type == 'scale')
        {
          ZW_MoveType = 'scale';
        }else if(type == 'rotation')
        {
          ZW_MoveType = 'rotation';
        }
        else if(type == 'remove')
        {
          RemoveTip(Current_Tip_Tag);
        }else
        {
          // jd.showToast({
          //   title: type,
          //   icon: 'success',
          //   duration: 500
          // })
        }
      }
      
    //ChangeView('bottom');
    //AddBackgroud(4);

    // ChangeView('top');
    // SetImageUrl('body', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/body/body03.png');

    // SetImageUrl('cloth', 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_vansdress_jd/jacket/jacket01.png');
    // SetImageUrl('hat', '');


    setTimeout(() => {
      ZW_ClickNodeName = '';
    }, 350);

    setTimeout(() => {
      this.longTap(e);
    }, 500);
  },
  onTouchMove(e) {
    if (gIsIphone) {
      // ZW_PixelRatio = 1;
      // e.touches[0].y -= ZW_Info.screenHeight - ZW_Info.windowHeight;
    }


    var clickPos = { x: e.touches[0].x, y: e.touches[0].y };

    if(ZW_MoveType != '')
    {
      var xOffset = clickPos.x - ZW_ClickPos.x;
      var yOffset = clickPos.y - ZW_ClickPos.y;

      if(ZW_MoveType == 'position')
      {
        var testPos = GetTipPosition(Current_Tip_Tag);
        SetTipPosition(Current_Tip_Tag,testPos[0]+xOffset,testPos[1]+yOffset);
      }else if(ZW_MoveType == 'rotation')
      {
        var testPos = GetTipPosition(Current_Tip_Tag);
        
        //计算角度
        let A = {X:testPos[0],Y:testPos[1]}
        let B = {X:clickPos.x,Y:clickPos.y}
        let C = {X:ZW_ClickPos.x,Y:ZW_ClickPos.y}
        var AB = Math.sqrt(Math.pow(A.X - B.X, 2) + Math.pow(A.Y - B.Y, 2));
        var AC = Math.sqrt(Math.pow(A.X - C.X, 2) + Math.pow(A.Y - C.Y, 2));
        var BC = Math.sqrt(Math.pow(B.X - C.X, 2) + Math.pow(B.Y - C.Y, 2));
        var cosA = (
                    Math.pow(AB, 2) + Math.pow(AC, 2) - Math.pow(BC, 2)
                  ) / (
                    2 * AB * AC
                  );
        var angleA = Math.round( Math.acos(cosA) * 180 / Math.PI );

        
        //计算是否为顺时针
        var bAdd = true;
        
        var p1 = { x: ZW_ClickPos.x, y: ZW_ClickPos.y };
        var p2 = { x: testPos[0], y: testPos[1] };
        var p3 = { x: clickPos.x, y: clickPos.y };
        bAdd = ((p1.x - p3.x) * (p2.y - p3.y) - (p1.y - p3.y) * (p2.x - p3.x)) < 0;

        var testRot = GetTipRotation(Current_Tip_Tag);
        if(bAdd)
        {
          testRot += angleA;
        }else
        {
          testRot -= angleA;
        }
          
        SetTipRotation(Current_Tip_Tag,testRot);
        
      }else if(ZW_MoveType == 'scale')
      {
        var testPos = GetTipScale(Current_Tip_Tag);
        if(xOffset > 0 )
        {
          SetTipScale(Current_Tip_Tag,testPos*1.05);
        }else
        {
          SetTipScale(Current_Tip_Tag,testPos*0.95);
        }
        
      }
    }

    ZW_ClickPos = { x: e.touches[0].x, y: e.touches[0].y };

    //console.log(e.touches[0].x / ZW_PixelRatio - ZW_ClickPos.x);

    //ZW_CurrentPos += (e.touches[0].x - ZW_ClickPos.x) ;


    //ZW_ClickPos.x = e.touches[0].x / ZW_PixelRatio;

    // if (ZW_HoldNodeName != '') {
    //   var clickPos = { x: e.touches[0].x / ZW_PixelRatio, y: e.touches[0].y / ZW_PixelRatio };

    //   //var resultIndex = -1;
    //   var resultDis = 100000;

    //   for (var i = 0; i < ZW_PointsArray[0].length; i++) {
    //     var dis = M_distance([ZW_PointsArray[0][i].x, ZW_PointsArray[0][i].y, 0], [clickPos.x, clickPos.y, 0]);
    //     if (dis < resultDis) {
    //       resultDis = dis;
    //       ZW_HoldIndex = i;
    //     }
    //   }
    // }
  },
  onTouchEnd(e) {
    //console.log(e);

    ZW_MoveType = '';

    ZW_bHold = false;
    if (ZW_HoldNodeName != '') {
      ReleaseProcess();
      ZW_HoldNodeName = '';
    }
  },
  onTouchCancel(e) {
    //console.log(e);
  },
  longTap(e) {

    if (!ZW_bHold) return;
    // console.log(e);
    if (gIsIphone) {
      ZW_PixelRatio = 1;
      //e.touches[0].y -= ZW_Info.screenHeight-ZW_Info.windowHeight;
    }

  },

  onGetShot() {
    // 将生成的canvas图片，转为真实图片
    //console.log("onGetShot() ----------------------");

    SetImageVisible('back1',true);  //显示背景图
    SetEditState(false);  //隐藏编辑框
    ShowProcess();
    jd.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: ZW_Info.windowWidth,
      height: ZW_Info.windowWidth*1.76,
      canvasId: 'canvas',
      success: function (resp) {
        SetImageVisible('back1',false);
        SetEditState(true); //显示原来的编辑框
        ShowProcess();

        jd.saveImageToPhotosAlbum({
          filePath: resp.tempFilePath,
          success(res) {
            jd.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 1500
            })
          }
        })
      },
      fail: function (res) {
      }
    });
    return;
  }
});